package com.fdb.efp.console.executor.impl;

import com.fdb.basic.framework.core.util.BeanUtility;
import com.fdb.efp.console.common.vo.EngineVO;
import com.fdb.efp.console.executor.RoutePluginExecutor;
import com.fdb.efp.console.service.vo.BizFlowRouteVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
 * 
 * 路由插件参数执行器
 * 
 * @author Cytus_
 * @since 2018年5月25日 上午10:04:53
 * @version 1.0
 *
 */
public abstract class AbstractRoutePluginExecutor implements RoutePluginExecutor {
	
	public final static String EVO_OBJECT = "evo";
	
	public final static String ROUTE_OBJECT = "route";
	
	private static Logger logger = LoggerFactory.getLogger(AbstractRoutePluginExecutor.class);

	public boolean judgeRoute(EngineVO evo, BizFlowRouteVO routeVo) {

		String[] registerArgs = this.getRegisterArgs();
		
		logger.info("当前路由插件注册的执行参数为:"+ Arrays.asList(registerArgs));
		
		if (Objects.nonNull(registerArgs) && registerArgs.length > 0) {
			
			Map<String, String> params = new HashMap<String, String>();
			try {
				for (String arg : registerArgs) {
					String newArg = new String(arg).toLowerCase();
					String key = arg.substring(arg.indexOf(".") + 1);
					Object object = newArg.startsWith(EVO_OBJECT) ? evo : newArg.startsWith(ROUTE_OBJECT) ? routeVo : null;
					if (Objects.nonNull(object)) {
						Object value = BeanUtility.getProptery(object, key);
						params.put(arg, Objects.nonNull(value) ? String.valueOf(value) : null);
					}
				}
				logger.info("当前路由插件注册的执行传入的参数为:"+ params);
				return execute(params);
			} catch (Exception e) {
				logger.error("路由插件注册参数获取出现异常!", e);
			}
		}
		
		return false;
	}
	
	/**
	 * 路由插件所需要的参数, 
	 * @return
	 */
	public abstract String[] getRegisterArgs() ;
	
	/**
	 * 执行
	 * @param params
	 * @return
	 * @throws Exception
	 */
	public abstract boolean execute(Map<String, String> params) throws Exception;
}
